{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "# logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)\n",
    "# logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Docugami\n",
    "This notebook covers how to load documents from `Docugami`. See [README](./README.md) for more details, and the advantages of using this system over alternative data readers.\n",
    "\n",
    "## Prerequisites\n",
    "1. Follow the Quick Start section in [README](./README.md)\n",
    "2. Grab an access token for your workspace, and make sure it is set as the DOCUGAMI_API_KEY environment variable\n",
    "3. Grab some docset and document IDs for your processed documents, as described here: https://help.docugami.com/home/docugami-api"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Documents\n",
    "\n",
    "If the DOCUGAMI_API_KEY environment variable is set, there is no need to pass it in to the reader explicitly otherwise you can pass it in as the `access_token` parameter.\n",
    "\n",
    "The DocugamiReader has a default minimum chunk size of 32. Chunks smaller than that are appended to subsequent chunks. Set min_chunk_size to 0 to get all structural chunks regardless of size."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Doc ID: d50a3b34-78ee-46d0-8091-68d24e093e14\n",
      "Text: UNITED STATES SECURITIES AND EXCHANGE COMMISSION\n",
      "********************************\n",
      "Doc ID: 1a7bece8-c4f6-44d2-9066-d6d03709927f\n",
      "Text: Washington , D.C. 20549  FORM 10-Q\n",
      "********************************\n",
      "Doc ID: b3b072c7-56b7-4190-859d-665ddc0cf240\n",
      "Text: ( Mark One )  ☒ QUARTERLY REPORT PURSUANT TO SECTION 13 OR 15(d)\n",
      "OF THE SECURITIES EXCHANGE ACT OF 1934\n",
      "********************************\n",
      "Doc ID: 0dd21505-f2eb-4d25-a481-8d212f4f4bf2\n",
      "Text: For the quarterly period ended June 25, 2022\n",
      "********************************\n",
      "Doc ID: d5a468dd-2274-40ef-a82c-3676f2adcdd4\n",
      "Text: or  ☐ TRANSITION REPORT PURSUANT TO SECTION 13 OR 15(d) OF THE\n",
      "SECURITIES EXCHANGE ACT OF 1934\n",
      "********************************\n"
     ]
    }
   ],
   "source": [
    "from base import DocugamiReader\n",
    "\n",
    "docset_id = \"tjwrr2ekqkc3\"\n",
    "docset_name = \"SEC 10-Q reports\"\n",
    "document_ids = [\"ui7pkriyckwi\", \"1be3o7ch10iy\"]\n",
    "\n",
    "reader = DocugamiReader()\n",
    "chunks = reader.load_data(docset_id=docset_id, document_ids=document_ids)\n",
    "\n",
    "for chunk in chunks[:5]:\n",
    "    print(chunk)\n",
    "    print(\"*\" * 32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `metadata` for each `Document` (really, a chunk of an actual PDF, DOC or DOCX) contains some useful additional information:\n",
    "\n",
    "1. **id and source:** ID and Name of the file (PDF, DOC or DOCX) the chunk is sourced from within Docugami.\n",
    "2. **xpath:** XPath inside the XML representation of the document, for the chunk. Useful for source citations directly to the actual chunk inside the document XML.\n",
    "3. **structure:** Structural attributes of the chunk, e.g. h1, h2, div, table, td, etc. Useful to filter out certain kinds of chunks if needed by the caller.\n",
    "4. **tag:** Semantic tag for the chunk, using various generative and extractive techniques. More details here: https://github.com/docugami/DFM-benchmarks\n",
    "\n",
    "You can control chunking behavior by setting the following properties on the `DocugamiReader` instance:\n",
    "\n",
    "1. You can set min and max chunk size, which the system tries to adhere to with minimal truncation. You can set `reader.min_text_length` and `reader.max_text_length` to control these.\n",
    "2. By default, only the text for chunks is returned. However, Docugami's XML knowledge graph has additional rich information including semantic tags for entities inside the chunk. Set `reader.include_xml_tags = True` if you want the additional xml metadata on the returned chunks.\n",
    "3. In addition, you can set `reader.parent_hierarchy_levels` if you want Docugami to return parent chunks in the chunks it returns. The child chunks point to the parent chunks via the `reader.parent_id_key` value. This is useful for [small-to-big](https://www.youtube.com/watch?v=ihSiRrOUwmg) retrieval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Doc ID: 790a9137-3c6d-4507-9c1f-b45ea68f8031\n",
      "Text: UNITED STATES SECURITIES AND EXCHANGE COMMISSION\n",
      "<USState>Washington</USState>, D.C. <ZipCode>20549 </ZipCode>  FORM\n",
      "10-Q  (Mark One)  <ReportingPeriod> ☒ QUARTERLY REPORT PURSUANT TO\n",
      "SECTION 13 OR 15(d) OF THE SECURITIES EXCHANGE ACT OF <Act>1934\n",
      "</Act></ReportingPeriod>  <FinancialReportingPeriod>\n",
      "<FinancialReportingPeriod>For the quarterly p...\n",
      "********************************\n",
      "Doc ID: 0aa728fc-1683-46d8-b69a-7bcf1e7efb84\n",
      "Text: Yes ☒ No ☐  <CompanySize> Indicate by check mark whether the\n",
      "Registrant is a large accelerated filer, an accelerated filer, a non-\n",
      "accelerated filer, a smaller reporting company, or an emerging growth\n",
      "company. See the definitions of “large accelerated filer,”\n",
      "“accelerated filer,” “smaller reporting company,” and “emerging growth\n",
      "company” in Rule ...\n",
      "********************************\n",
      "Doc ID: 091992e1-5dbb-4804-8b00-6776d682aec1\n",
      "Text: <FinancialStatementNotes> See accompanying Notes to Condensed\n",
      "Consolidated Financial Statements. </FinancialStatementNotes>\n",
      "<CompanyNameandFilingDate> <CompanyName>Apple Inc. </CompanyName>|\n",
      "<Quarter>Q3 </Quarter><Year>2022 </Year>Form 10-Q | <CompanyName>1\n",
      "</CompanyName></CompanyNameandFilingDate>  CONDENSED CONSOLIDATED\n",
      "STATEMENTS OF COMPREHE...\n",
      "********************************\n",
      "Doc ID: d85b417b-624d-496c-b34d-ec50d7527517\n",
      "Text: <FinancialStatementNotes> See accompanying Notes to Condensed\n",
      "Consolidated Financial Statements. </FinancialStatementNotes>\n",
      "<FinancialStatements> <CompanyName>Apple Inc. </CompanyName>|\n",
      "<Quarter>Q3 </Quarter><Year>2022 </Year>Form 10-Q | <CompanyName>2\n",
      "</CompanyName></FinancialStatements>  CONDENSED CONSOLIDATED BALANCE\n",
      "SHEETS (Unaudited)  <Fin...\n",
      "********************************\n",
      "Doc ID: 31a40f16-6e6e-49a6-848c-355db0b6008c\n",
      "Text: <FinancialStatementNotes> See accompanying Notes to Condensed\n",
      "Consolidated Financial Statements. </FinancialStatementNotes>\n",
      "<FinancialStatements> <CompanyName>Apple Inc. </CompanyName>|\n",
      "<Quarter>Q3 </Quarter><Year>2022 </Year>Form 10-Q |\n",
      "<Form10-QFilingDate>3 </Form10-QFilingDate></FinancialStatements>\n",
      "CONDENSED CONSOLIDATED STATEMENTS OF SHAR...\n",
      "********************************\n"
     ]
    }
   ],
   "source": [
    "reader.min_text_length = 1024 * 4  # ~1k tokens\n",
    "reader.max_text_length = 1024 * 24  # ~6k tokens\n",
    "reader.include_xml_tags = True\n",
    "chunks = reader.load_data(docset_id=docset_id)\n",
    "\n",
    "for chunk in chunks[:5]:\n",
    "    print(chunk)\n",
    "    print(\"*\" * 32)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Use: Docugami Reader for Document QA\n",
    "\n",
    "You can use the Docugami Reader like a standard reader for Document QA over multiple docs, albeit with much better chunks that follow the natural contours of the document. There are many great tutorials on how to do this, e.g. [this one](https://gpt-index.readthedocs.io/en/latest/getting_started/starter_example.html). We can just use the same code, but use the `DocugamiReader` for better chunking, instead of loading text or PDF files directly with basic splitting techniques."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The documents returned by the reader are already split into chunks. Optionally, we can use the metadata on each chunk, for example the structure or tag attributes, to do any post-processing we want.\n",
    "\n",
    "We will just use the output of the `DocugamiReader` as-is to set up a query engine the usual way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import chromadb\n",
    "from llama_index import VectorStoreIndex, ServiceContext\n",
    "from llama_index.embeddings import OpenAIEmbedding\n",
    "from llama_index.storage.storage_context import StorageContext\n",
    "from llama_index.vector_stores import ChromaVectorStore\n",
    "\n",
    "db = chromadb.PersistentClient(path=\"/tmp/docugami/chroma_db\")\n",
    "chroma_collection = db.get_or_create_collection(\"docugami_test\")\n",
    "embed_model = OpenAIEmbedding()\n",
    "\n",
    "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n",
    "storage_context = StorageContext.from_defaults(vector_store=vector_store)\n",
    "service_context = ServiceContext.from_defaults(embed_model=embed_model)\n",
    "\n",
    "index = VectorStoreIndex.from_documents(\n",
    "    chunks, storage_context=storage_context, service_context=service_context\n",
    ")\n",
    "\n",
    "query_engine = index.as_query_engine(similarity_top_k=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Microsoft spent $119 million for operating expenses in the latest quarter.\n"
     ]
    }
   ],
   "source": [
    "# Try out the query engine with example query\n",
    "response = query_engine.query(\n",
    "    \"How much did Microsoft spend for opex in the latest quarter?\"\n",
    ")\n",
    "print(response.response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023 Q2 MSFT.pdf\n",
      "<OperatingExpenses> Operating expenses increased <OperatingExpenseIncrease>$996 million </OperatingExpenseIncrease>or <OperatingExpenseGrowth>7% </OperatingExpenseGrowth>driven by <OperatingExpenses>2 </OperatingExpenses>points of growth from the Nuance and <AcquisitionGrowth>Xandr </AcquisitionGrowth>acquisitions and investments in cloud engineering and <LinkedInInvestments>LinkedIn</LinkedInInvestments>. </OperatingExpenses>\n",
      " Key changes in operating expenses were:\n",
      " • Research and development expenses increased <RDExpenseIncrease>$678 million </RDExpenseIncrease>or <RDExpenseGrowth>11% </RDExpenseGrowth>driven by investments in cloud engineering and <RDInvestments>LinkedIn</RDInvestments>.\n",
      " • <SalesandMarketingExpenses>Sales and marketing expenses increased <SalesandMarketingExpenseIncrease>$155 million </SalesandMarketingExpenseIncrease>or <SalesandMarketingExpenseGrowth>3% </SalesandMarketingExpenseGrowth>driven by <SalesandMarketingExpenseGrowth>3 </SalesandMarketingExpenseGrowth>points of growth from the Nuance and <AcquisitionImpact>Xandr </AcquisitionImpact>acquisitions and investments in commercial sales, offset in part by declines in Windows and Devices advertising. </SalesandMarketingExpenses>\n",
      "2023 Q1 MSFT.pdf\n",
      "• <Org>Microsoft </Org>Cloud gross margin percentage increased <MicrosoftCloudGrossMarginPercentage>2 </MicrosoftCloudGrossMarginPercentage>points to <MicrosoftCloudGrossMarginPercentage>72%</MicrosoftCloudGrossMarginPercentage>. Excluding the impact of the change in accounting estimate, <CloudGrossMarginPercentage>Microsoft </CloudGrossMarginPercentage>Cloud gross margin percentage decreased <MicrosoftCloudGrossMarginPercentage>1 </MicrosoftCloudGrossMarginPercentage>point driven by sales mix shift to <AzureGrossMarginPercentage>Azure </AzureGrossMarginPercentage>and other cloud services and higher energy costs.\n",
      " <OperatingExpensesIncrease> Operating expenses increased <OperatingExpenseIncrease>$2.3 billion </OperatingExpenseIncrease>or <OperatingExpenseIncrease>19% </OperatingExpenseIncrease>driven by employee severance expenses, investments in cloud engineering, the Nuance acquisition, and <LinkedInAcquisition>LinkedIn</LinkedInAcquisition>. </OperatingExpensesIncrease>\n",
      " Key changes in operating expenses were:\n",
      " • Research and development expenses increased <ResearchandDevelopmentExpenses>$1.1 billion </ResearchandDevelopmentExpenses>or <ResearchandDevelopmentExpenseIncrease>19% </ResearchandDevelopmentExpenseIncrease>driven by investments in cloud engineering, impairment charges resulting from changes to our hardware portfolio, and <LinkedInImpact>LinkedIn</LinkedInImpact>. Research and development included a favorable foreign currency impact of <ForeignCurrencyImpact>2%</ForeignCurrencyImpact>.\n",
      "2023 Q1 MSFT.pdf\n",
      "<Non-GAAPFinancialMeasures> Adjusted gross margin, operating income, net income, and diluted earnings per share (“EPS”) are non-GAAP financial measures. Current year non-GAAP financial measures exclude the impact of the Q2 charge, which includes employee severance expenses, impairment charges resulting from changes to our hardware portfolio, and costs related to lease consolidation activities. Prior year non-GAAP financial measures exclude the net income tax benefit related to transfer of intangible properties in the first <Non-GAAPFinancialMeasures>quarter of fiscal year 2022</Non-GAAPFinancialMeasures>. Refer to Note 11 – Income Taxes of the Notes to Financial Statements (Part I, Item 1 of this Form 10-Q) for further discussion. Refer to the Non-GAAP Financial Measures section below for a reconciliation of our financial results reported in accordance with GAAP to non-GAAP financial results. </Non-GAAPFinancialMeasures>\n",
      " <ThreeMonthsEnded>Three Months </ThreeMonthsEnded>Ended <EndDate>December 31, 2022 </EndDate>Compared with <TimeDuration>Three Months </TimeDuration>Ended <ComparisonDate>December 31, 2021 </ComparisonDate>\n",
      " <RevenueGrowth> Revenue increased <RevenueIncrease>$1.0 billion </RevenueIncrease>or <RevenueGrowth>2% </RevenueGrowth>driven by growth in <IntelligentCloudRevenue>Intelligent </IntelligentCloudRevenue>Cloud and Productivity and Business Processes, offset in part by a decline in More Personal Computing. Intelligent Cloud revenue increased driven by <AzureRevenue>Azure </AzureRevenue>and other cloud services. Productivity and Business Processes revenue increased driven by Office 365 Commercial. More Personal Computing revenue decreased driven by declines in Windows, Devices, and Gaming. </RevenueGrowth>\n",
      " <CostofRevenueIncrease> Cost of revenue increased <CostofRevenueIncrease>$528 million </CostofRevenueIncrease>or <CostofRevenueGrowth>3% </CostofRevenueGrowth>driven by growth in <CostofRevenue>Microsoft </CostofRevenue>Cloud, offset in part by a reduction in depreciation expense due to the change in accounting estimate for the useful lives of our server and network equipment. </CostofRevenueIncrease>\n",
      " <GrossMarginIncrease> Gross margin increased <GrossMarginIncrease>$491 million </GrossMarginIncrease>or <GrossMarginChange>1% </GrossMarginChange>driven by growth in Intelligent Cloud and Productivity and Business Processes and the change in accounting estimate, offset in part by a decline in More Personal Computing. </GrossMarginIncrease>\n",
      " • Gross margin percentage decreased slightly. Excluding the impact of the change in accounting estimate, gross margin percentage decreased <GrossMarginPercentage>2 </GrossMarginPercentage>points driven by reductions in More Personal Computing and <IntelligentCloudGrossMargin>Intelligent </IntelligentCloudGrossMargin>Cloud, offset in part by sales mix shift.\n",
      " • <Org>Microsoft </Org>Cloud gross margin percentage increased <MicrosoftCloudGrossMarginPercentage>2 </MicrosoftCloudGrossMarginPercentage>points to <MicrosoftCloudGrossMarginPercentage>72%</MicrosoftCloudGrossMarginPercentage>. Excluding the impact of the change in accounting estimate, <CloudGrossMarginPercentage>Microsoft </CloudGrossMarginPercentage>Cloud gross margin percentage decreased <MicrosoftCloudGrossMarginPercentage>1 </MicrosoftCloudGrossMarginPercentage>point driven by sales mix shift to <AzureGrossMarginPercentage>Azure </AzureGrossMarginPercentage>and other cloud services and higher energy costs.\n",
      " <OperatingExpensesIncrease> Operating expenses increased <OperatingExpenseIncrease>$2.3 billion </OperatingExpenseIncrease>or <OperatingExpenseIncrease>19% </OperatingExpenseIncrease>driven by employee severance expenses, investments in cloud engineering, the Nuance acquisition, and <LinkedInAcquisition>LinkedIn</LinkedInAcquisition>.\n",
      "2023 Q3 MSFT.pdf\n",
      "• Operating expenses increased <OperatingExpenseIncrease>$119 million </OperatingExpenseIncrease>or <OperatingExpenseIncrease>2% </OperatingExpenseIncrease>primarily driven by <OperatingExpenseDriver>LinkedIn</OperatingExpenseDriver>.\n",
      " <IntelligentCloud>Intelligent </IntelligentCloud><Cloud>Cloud </Cloud>\n",
      " Revenue increased <RevenueIncrease>$3.9 billion </RevenueIncrease>or <RevenueGrowth>19%</RevenueGrowth>.\n",
      " • Server products and cloud services revenue increased <ServerProductsandCloudServicesRevenue>$3.9 billion </ServerProductsandCloudServicesRevenue>or <ServerProductsandCloudServicesRevenueGrowth>21% </ServerProductsandCloudServicesRevenueGrowth>driven by <AzureRevenue>Azure </AzureRevenue>and other cloud services. Azure and other cloud services revenue grew <AzureRevenueGrowth>29% </AzureRevenueGrowth>driven by growth in our consumption-based services. Server products revenue increased <ServerProductsRevenueGrowth>2% </ServerProductsRevenueGrowth>driven by demand for <WindowsServerRevenue>Windows </WindowsServerRevenue>Server and <SQLServerRevenue>SQL </SQLServerRevenue>Server running in multi-cloud environments, offset in part by continued customer shift to cloud offerings.\n",
      "2023 Q1 MSFT.pdf\n",
      "Operating income decreased <OperatingIncomeDecrease>$3.7 billion </OperatingIncomeDecrease>or <OperatingIncomeDecrease>33%</OperatingIncomeDecrease>.\n",
      " • Gross margin decreased <GrossMarginDecrease>$3.4 billion </GrossMarginDecrease>or <GrossMarginDecrease>20% </GrossMarginDecrease>driven by declines in Windows, Devices, and Gaming. Gross margin percentage decreased driven by reductions in Devices, including the impact of impairment charges resulting from changes to our hardware portfolio, and sales mix shift to lower margin businesses.\n",
      " • Operating expenses increased <OperatingExpenseIncrease>$269 million </OperatingExpenseIncrease>or <OperatingExpenseIncrease>4% </OperatingExpenseIncrease>driven by investment in Search and news advertising and employee severance expenses, offset in part by a decline in Devices.\n",
      " <Number>39 </Number>\n",
      " <_39> Revenue, gross margin, and operating income included an unfavorable foreign currency impact of <ForeignCurrencyImpact>3%</ForeignCurrencyImpact>, <ForeignCurrencyImpact>5%</ForeignCurrencyImpact>, and <ForeignCurrencyImpact>7%</ForeignCurrencyImpact>, respectively. Operating expenses included a favorable foreign currency impact of <Percent>3%</Percent>.\n"
     ]
    }
   ],
   "source": [
    "for node in response.source_nodes:\n",
    "    print(node.node.extra_info[\"name\"])\n",
    "    print(node.node.text)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Docugami to Add Metadata to Chunks for High Accuracy Document QA\n",
    "\n",
    "One issue with large documents is that the correct answer to your question may depend on chunks that are far apart in the document. Typical chunking techniques, even with overlap, will struggle with providing the LLM sufficent context to answer such questions. With upcoming very large context LLMs, it may be possible to stuff a lot of tokens, perhaps even entire documents, inside the context but this will still hit limits at some point with very long documents, or a lot of documents.\n",
    "\n",
    "For example, if we ask a more complex question that requires the LLM to draw on chunks from different parts of the document, even OpenAI's powerful LLM is unable to answer correctly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Microsoft's weighted average discount rate for operating leases as of March 2023 was 3.21%.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\n",
    "    \"What was Microsoft's weighted average discount rate for operating leases as of March 2023?\"\n",
    ")\n",
    "print(\n",
    "    response.response\n",
    ")  # the correct answer should be 2.7%, listed on page 24 of \"2023 Q2 MSFT.pdf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023 Q1 MSFT.pdf\n",
      "at cost </OperatingLeasesPropertyAndEquipmentatCost></td> <td><PropertyAndEquipmentatCostDecember312022><Money>$ 18,514 </Money> <Money>$ </Money></PropertyAndEquipmentatCostDecember312022></td> <td><PropertyAndEquipmentatCostJune302022>17,388 </PropertyAndEquipmentatCostJune302022></td></tr> <tr><td><OperatingLeasesAccumulatedDepreciation>Accumulated depreciation </OperatingLeasesAccumulatedDepreciation></td> <td><AccumulatedDepreciationDecember312022>(<Number>3,920</Number>) </AccumulatedDepreciationDecember312022></td> <td><AccumulatedDepreciationJune302022>(<Number>3,285</Number>) </AccumulatedDepreciationJune302022></td></tr><tr><td><OperatingLeases/></td></tr> <tr><td><OperatingLeasesPropertyAndEquipmentNet>Property and equipment, net </OperatingLeasesPropertyAndEquipmentNet></td> <td><PropertyAndEquipmentNetDecember312022><Money>$ 14,594 </Money> <Money>$ </Money></PropertyAndEquipmentNetDecember312022></td> <td><PropertyAndEquipmentNetJune302022>14,103 </PropertyAndEquipmentNetJune302022></td></tr> <tr><td><OperatingLeasesOtherCurrentLiabilities>Other current liabilities </OperatingLeasesOtherCurrentLiabilities></td> <td><OtherCurrentLiabilitiesDecember312022><Money>$ 1,105 </Money> <Money>$ </Money></OtherCurrentLiabilitiesDecember312022></td> <td><OtherCurrentLiabilitiesJune302022>1,060 </OtherCurrentLiabilitiesJune302022></td></tr> <tr><td><OperatingLeasesOtherLong-termLiabilities>Other long-term liabilities </OperatingLeasesOtherLong-termLiabilities></td> <td><OtherLong-termLiabilitiesDecember312022>14,479 </OtherLong-termLiabilitiesDecember312022></td> <td><OtherLong-termLiabilitiesJune302022>13,842 </OtherLong-termLiabilitiesJune302022></td></tr> <tr><td><OperatingLeasesTotalFinanceLeaseLiabilities>Total finance lease liabilities </OperatingLeasesTotalFinanceLeaseLiabilities></td> <td><TotalFinanceLeaseLiabilitiesDecember312022><Money>$ 15,584 </Money> <Money>$ </Money></TotalFinanceLeaseLiabilitiesDecember312022></td> <td><TotalFinanceLeaseLiabilitiesJune302022>14,902 </TotalFinanceLeaseLiabilitiesJune302022></td></tr> <tr><td><OperatingLeasesWeightedAverageRemainingLeaseTerm>Weighted Average Remaining Lease Term </OperatingLeasesWeightedAverageRemainingLeaseTerm></td><td/><td><WeightedAverageRemainingLeaseTermJune302022/></td></tr> <tr><td><OperatingLeasesOperatingLeases>Operating leases </OperatingLeasesOperatingLeases></td> <td><OperatingLeasesDecember312022>8 years </OperatingLeasesDecember312022></td> <td><OperatingLeasesJune302022>8 years </OperatingLeasesJune302022></td></tr> <tr><td><OperatingLeasesFinanceLeases>Finance leases </OperatingLeasesFinanceLeases></td> <td><FinanceLeasesDecember312022>11 years </FinanceLeasesDecember312022></td> <td><FinanceLeasesJune302022>12 years </FinanceLeasesJune302022></td></tr> <tr><td><OperatingLeasesWeightedAverageDiscountRate>Weighted Average Discount Rate </OperatingLeasesWeightedAverageDiscountRate></td><td/><td><WeightedAverageDiscountRateJune302022/></td></tr> <tr><td><OperatingLeasesOperatingLeases>Operating leases </OperatingLeasesOperatingLeases></td> <td><OperatingLeasesDecember312022>2.\n",
      "2023 Q2 NVDA.pdf\n",
      "</OperatingLeaseCommitments>\n",
      " <OperatingLeaseExpenses> Operating lease expenses were <OperatingLeaseExpenses>$67 million </OperatingLeaseExpenses>and <OperatingLeaseExpenses>$47 million </OperatingLeaseExpenses>for the second quarter of fiscal years <OperatingLeaseExpenses>2024 </OperatingLeaseExpenses>and 2023, respectively, and <OperatingLeaseExpenses>$126 million </OperatingLeaseExpenses>and <OperatingLeaseExpenses>$90 million </OperatingLeaseExpenses>for the first half of fiscal years 2024 and 2023, respectively. Short-term and variable lease expenses for the second quarter and first half of fiscal years 2024 and <OperatingLeaseExpenses>2023 </OperatingLeaseExpenses>were not significant. </OperatingLeaseExpenses>\n",
      " <Number>10 </Number>\n",
      " Other information related to leases was as follows:\n",
      " <table><tbody><tr><td><SixMonthsEnded-cell><SixMonthsEnded>Six Months </SixMonthsEnded>Ended <July302023-section> <Deadline>July 30, 2023 </Deadline><July302023> <Deadline>July 31, 2022 </Deadline> (In millions) </July302023></July302023-section></SixMonthsEnded-cell></td></tr> <tr><td><SixMonthsEndedJuly302023July312022InMillionsSupplementalCashFlowsI>Supplemental cash flows information </SixMonthsEndedJuly302023July312022InMillionsSupplementalCashFlowsI></td></tr> <tr><td><SixMonthsEndedJuly302023July312022InMillionsOperatingCashFlowsUsed>Operating cash flows used for operating leases </SixMonthsEndedJuly302023July312022InMillionsOperatingCashFlowsUsed></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 135 </OperatingCashFlowsUsedforOperatingLeases></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 91 </OperatingCashFlowsUsedforOperatingLeases></td></tr> <tr><td><SixMonthsEndedJuly302023July312022InMillionsOperatingLeaseAssetsOb>Operating lease assets obtained in exchange for lease obligations </SixMonthsEndedJuly302023July312022InMillionsOperatingLeaseAssetsOb></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 299 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 98 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td></tr></tbody></table>\n",
      " <OperatingLeaseDetails> As of <LeaseTerm>July 30, 2023</LeaseTerm>, our operating leases had a weighted average remaining lease term of <RemainingLeaseTerm>6.5 years </RemainingLeaseTerm>and a weighted average discount rate of <WeightedAverageDiscountRate>3.47%</WeightedAverageDiscountRate>.\n",
      "2023 Q1 NVDA.pdf\n",
      "</OperatingLeaseCommitments>\n",
      " <OperatingLeaseExpenses> Operating lease expenses were <OperatingLeaseExpenses>$59 million </OperatingLeaseExpenses>and <OperatingLeaseExpenses>$44 million </OperatingLeaseExpenses>for the first quarter of fiscal years <OperatingLeaseExpenses>2024 </OperatingLeaseExpenses>and 2023, respectively. Short-term and variable lease expenses for the first quarter of fiscal years 2024 and <OperatingLeaseExpenses>2023 </OperatingLeaseExpenses>were not significant. </OperatingLeaseExpenses>\n",
      " Other information related to leases was as follows:\n",
      " <table><tbody><tr><td><ThreeMonthsEnded-section><ThreeMonthsEnded>Three Months </ThreeMonthsEnded>Ended <ThreeMonthsEnded> <Deadline>April 30, 2023 </Deadline><Deadline>May 1, 2022 </Deadline> (In millions) </ThreeMonthsEnded></ThreeMonthsEnded-section></td></tr> <tr><td><ThreeMonthsEndedApril302023May12022InMillionsSupplementalCashFlows>Supplemental cash flows information </ThreeMonthsEndedApril302023May12022InMillionsSupplementalCashFlows></td></tr> <tr><td><ThreeMonthsEndedApril302023May12022InMillionsOperatingCashFlowsUse>Operating cash flows used for operating leases </ThreeMonthsEndedApril302023May12022InMillionsOperatingCashFlowsUse></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 61 </OperatingCashFlowsUsedforOperatingLeases></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 45 </OperatingCashFlowsUsedforOperatingLeases></td></tr> <tr><td><ThreeMonthsEndedApril302023May12022InMillionsOperatingLeaseAssetsO>Operating lease assets obtained in exchange for lease obligations </ThreeMonthsEndedApril302023May12022InMillionsOperatingLeaseAssetsO></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 106 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 62 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td></tr></tbody></table>\n",
      " <Number>9 </Number>\n",
      " <OperatingLeaseDetails> As of <LeaseTerm>April 30, 2023</LeaseTerm>, our operating leases had a weighted average remaining lease term of <RemainingLeaseTerm>6.6 years </RemainingLeaseTerm>and a weighted average discount rate of <WeightedAverageDiscountRate>3.33%</WeightedAverageDiscountRate>. As of <LeaseTermandDiscountRate>January 29, 2023</LeaseTermandDiscountRate>, our operating leases had a weighted average remaining lease term of <WeightedAverageLeaseTerm>6.8 years </WeightedAverageLeaseTerm>and a weighted average discount rate of <WeightedAverageDiscountRate>3.21%</WeightedAverageDiscountRate>.\n",
      "2023 Q2 MSFT.pdf\n",
      "We expect to recognize approximately <RecognitionRate>45% </RecognitionRate>of this revenue over the next <RecognitionPeriod>12 months </RecognitionPeriod>and the remainder thereafter. </_39601>\n",
      " NOTE 13 — LEASES\n",
      " <LeaseTerms> We have operating and finance leases for datacenters, corporate offices, research and development facilities, <LeaseType>Microsoft Experience Centers</LeaseType>, and certain equipment. Our leases have remaining lease terms of less than <TimeDuration>1 year </TimeDuration>to <LeaseTerm>18 years</LeaseTerm>, some of which include options to extend the leases for up to <LeaseTerm>5 years</LeaseTerm>, and some of which include options to terminate the leases within <LeaseTerm>1 year</LeaseTerm>. </LeaseTerms>\n",
      " The components of lease expense were as follows:\n",
      " <table><tbody><tr><td>(In millions) </td> <td><ThreeMonthsEndedMarch31-cell><ThreeMonthsEnded>Three Months </ThreeMonthsEnded>Ended <March31-section> <Deadline>March 31</Deadline>, <March31> <Number>2023 </Number> <Number>2022 </Number></March31></March31-section></ThreeMonthsEndedMarch31-cell></td> <td><NineMonthsEnded-section><NineMonthsEnded>Nine Months </NineMonthsEnded>Ended <NineMonthsEnded> <Deadline>March 31</Deadline>, <March31> <Number>2023 </Number> <Number>2022 </Number></March31></NineMonthsEnded></NineMonthsEnded-section></td></tr> <tr><td><InMillions>Operating lease cost </InMillions></td> <td><OperatingLeaseCostThreeMonthsEndedMarch3120232022><Money>$ 766 </Money> <Money>$ </Money></OperatingLeaseCostThreeMonthsEndedMarch3120232022></td> <td><OperatingLeaseCostNineMonthsEndedMarch3120232022><Number>744 </Number> <Money>$ </Money></OperatingLeaseCostNineMonthsEndedMarch3120232022></td> <td><OperatingLeaseCost><Number>2,112 </Number> <Money>$ </Money></OperatingLeaseCost></td> <td><OperatingLeaseCost>1,\n",
      "2023 Q3 MSFT.pdf\n",
      "The estimated fair values are based on Level <EstimatedFairValueofLong-TermDebt>2 </EstimatedFairValueofLong-TermDebt>inputs. </Long-TermDebtFairValue>\n",
      " <DebtStructure> Debt in the table above is comprised of senior unsecured obligations and ranks equally with our other outstanding obligations. Interest is paid semi-<Frequency>annually</Frequency>, except for the Euro-denominated debt, which is paid <Frequency>annually</Frequency>. </DebtStructure>\n",
      " <Long-TermDebtMaturitySchedule> The following table outlines maturities of our long-term debt, including the current portion, as of <MaturityDate>September 30, 2023</MaturityDate>: </Long-TermDebtMaturitySchedule>\n",
      " (In millions)\n",
      " <table><tbody><tr><td>Year Ending <YearEndDate>June 30</YearEndDate>, </td><td><YearEndingJune30/></td></tr> <tr><td><YearEndingJune302024ExcludingtheThreeMonthsEndedSeptember302023><EndDate>2024 </EndDate>(excluding the <EndDate>three months </EndDate>ended <EndDate>September 30, 2023</EndDate>) </YearEndingJune302024ExcludingtheThreeMonthsEndedSeptember302023></td> <td><_2024ExcludingtheThreeMonthsEndedSeptember302023>$ 3,750 </_2024ExcludingtheThreeMonthsEndedSeptember302023></td></tr> <tr><td><YearEndingJune302025>2025 </YearEndingJune302025></td> <td><_2025>2,250 </_2025></td></tr> <tr><td><YearEndingJune302026>2026 </YearEndingJune302026></td> <td><_2026>3,000 </_2026></td></tr> <tr><td><YearEndingJune302027>2027 </YearEndingJune302027></td> <td><_2027>8,000 </_2027></td></tr> <tr><td><YearEndingJune302028>2028 </YearEndingJune302028></td> <td><_2028>0 </_2028></td></tr> <tr><td><YearEndingJune30Thereafter>Thereafter </YearEndingJune30Thereafter></td> <td><Thereafter>34,292 </Thereafter></td></tr><tr><td><YearEndingJune30/></td></tr> <tr><td><YearEndingJune30Total>Total </YearEndingJune30Total></td> <td><Total>$ 51,292 </Total></td></tr></tbody></table>\n",
      " <Number>19 </Number>\n",
      " NOTE 10 — INCOME TAXES\n",
      " Effective Tax Rate\n",
      " <TaxRate> Our effective tax rate was <EffectiveTaxRate>18% </EffectiveTaxRate>and <EffectiveTaxRate>19% </EffectiveTaxRate>for the <EffectiveTaxRate>three months </EffectiveTaxRate>ended <EffectiveTaxRate>September 30, 2023 </EffectiveTaxRate>and <EffectiveTaxRate>2022</EffectiveTaxRate>, respectively. The decrease in our effective tax rate for the current quarter compared to the prior year was primarily due to tax benefits from tax law changes in the <EffectiveTaxRate>first quarter of fiscal year 2024</EffectiveTaxRate>, including the impact from the issuance of Notice <EffectiveTaxRate>2023</EffectiveTaxRate>-<EffectiveTaxRate>55 </EffectiveTaxRate>by the <TaxLawChanges>Internal Revenue Service </TaxLawChanges>(“<IRSNotice>IRS”</IRSNotice>) and <USTreasuryDepartment>U.S. Treasury Department</USTreasuryDepartment>, which delayed the effective date of final foreign tax credit regulations to fiscal <EffectiveTaxRate>year </EffectiveTaxRate><EffectiveTaxRate>2024 </EffectiveTaxRate>for <EffectiveTaxRate>Microsoft</EffectiveTaxRate>. </TaxRate>\n"
     ]
    }
   ],
   "source": [
    "for node in response.source_nodes:\n",
    "    print(node.node.extra_info[\"name\"])\n",
    "    print(node.node.text)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At first glance the answer may seem plausible, but if you review the source chunks carefully for this answer, you will see that the chunking of the document did not end up putting the Landlord name and the rentable area in the same context, since they are far apart in the document. The query engine therefore ends up finding unrelated chunks from other documents not even related to the **Birch Street** landlord. That landlord happens to be mentioned on the first page of the file **TruTone Lane 1.docx** file, and none of the source chunks used by the query engine contain the correct answer (**$78,000**), and the answer is therefore incorrect."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Docugami can help here. Chunks are annotated with additional metadata created using different techniques if a user has been [using Docugami](https://help.docugami.com/home/reports). More technical approaches will be added later.\n",
    "\n",
    "Specifically, let's load the data again and this time let's ask the reader to include additional metadata that is returned on the documents returned by docugami after some additional use, in the form of some simple key/value pairs on all the text chunks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'xpath': '/dg:chunk/dg:chunk/dg:chunk[1]',\n",
       " 'id': '8d298e24da52d0b0220b9674f373f2fc',\n",
       " 'name': '2022 Q3 AAPL.pdf',\n",
       " 'structure': 'h1 h1 h1 p p p h1 p p p h1 p h1 p lim h1 p p h1 h1 h1 lim h1 lim h1 lim h1 lim lim h1 lim h1 lim h1 lim h1 lim h1 div lim p h1 h1 p h1 h1 div h1 h1 lim h1 p h1 div',\n",
       " 'tag': 'chunk ReportingPeriod FinancialReportingPeriod TransitionReport TransitionPeriod CompanyName Phone SecuritiesRegisteredPursuanttoSection12b TheNasdaqStockMarketLLC FilingCompliance',\n",
       " 'Financial Reporting Period': 'For the quarterly period ended  June 25, 2022',\n",
       " 'Company': 'Apple Inc.'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chunks[0].metadata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note semantic metadata tags like Lease Date, Landlord, Tenant, etc that are based on key chunks in the document even if they don't appear near the chunk in question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.indices.vector_store.retrievers import VectorIndexAutoRetriever\n",
    "from llama_index.vector_stores.types import (\n",
    "    MetadataInfo,\n",
    "    VectorStoreInfo,\n",
    "    VectorStoreQueryMode,\n",
    ")\n",
    "from llama_index.query_engine import RetrieverQueryEngine\n",
    "\n",
    "EXCLUDE_KEYS = [\"id\", \"xpath\", \"structure\", \"name\", \"tag\"]\n",
    "metadata_field_info = [\n",
    "    MetadataInfo(\n",
    "        name=key,\n",
    "        description=f\"The {key} for this chunk\",\n",
    "        type=\"str\",\n",
    "    )\n",
    "    for key in chunks[0].metadata\n",
    "    if key.lower() not in EXCLUDE_KEYS\n",
    "]\n",
    "\n",
    "vector_store_info = VectorStoreInfo(\n",
    "    content_info=f\"Key metadata about {docset_name}\",\n",
    "    metadata_info=metadata_field_info,\n",
    ")\n",
    "\n",
    "retriever = VectorIndexAutoRetriever(\n",
    "    index,\n",
    "    vector_store_info=vector_store_info,\n",
    ")\n",
    "\n",
    "query_engine_auto_retriever = RetrieverQueryEngine.from_args(retriever=retriever)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's run the same question again. It returns the correct result since all the chunks have metadata key/value pairs on them carrying key information about the document even if this information is physically very far away from the source chunk used to generate the answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Microsoft's weighted average discount rate for operating leases as of March 2023 was 3.21%.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\n",
    "    \"What was Microsoft's weighted average discount rate for operating leases as of March 2023?\"\n",
    ")\n",
    "print(response.response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023 Q1 MSFT.pdf\n",
      "at cost </OperatingLeasesPropertyAndEquipmentatCost></td> <td><PropertyAndEquipmentatCostDecember312022><Money>$ 18,514 </Money> <Money>$ </Money></PropertyAndEquipmentatCostDecember312022></td> <td><PropertyAndEquipmentatCostJune302022>17,388 </PropertyAndEquipmentatCostJune302022></td></tr> <tr><td><OperatingLeasesAccumulatedDepreciation>Accumulated depreciation </OperatingLeasesAccumulatedDepreciation></td> <td><AccumulatedDepreciationDecember312022>(<Number>3,920</Number>) </AccumulatedDepreciationDecember312022></td> <td><AccumulatedDepreciationJune302022>(<Number>3,285</Number>) </AccumulatedDepreciationJune302022></td></tr><tr><td><OperatingLeases/></td></tr> <tr><td><OperatingLeasesPropertyAndEquipmentNet>Property and equipment, net </OperatingLeasesPropertyAndEquipmentNet></td> <td><PropertyAndEquipmentNetDecember312022><Money>$ 14,594 </Money> <Money>$ </Money></PropertyAndEquipmentNetDecember312022></td> <td><PropertyAndEquipmentNetJune302022>14,103 </PropertyAndEquipmentNetJune302022></td></tr> <tr><td><OperatingLeasesOtherCurrentLiabilities>Other current liabilities </OperatingLeasesOtherCurrentLiabilities></td> <td><OtherCurrentLiabilitiesDecember312022><Money>$ 1,105 </Money> <Money>$ </Money></OtherCurrentLiabilitiesDecember312022></td> <td><OtherCurrentLiabilitiesJune302022>1,060 </OtherCurrentLiabilitiesJune302022></td></tr> <tr><td><OperatingLeasesOtherLong-termLiabilities>Other long-term liabilities </OperatingLeasesOtherLong-termLiabilities></td> <td><OtherLong-termLiabilitiesDecember312022>14,479 </OtherLong-termLiabilitiesDecember312022></td> <td><OtherLong-termLiabilitiesJune302022>13,842 </OtherLong-termLiabilitiesJune302022></td></tr> <tr><td><OperatingLeasesTotalFinanceLeaseLiabilities>Total finance lease liabilities </OperatingLeasesTotalFinanceLeaseLiabilities></td> <td><TotalFinanceLeaseLiabilitiesDecember312022><Money>$ 15,584 </Money> <Money>$ </Money></TotalFinanceLeaseLiabilitiesDecember312022></td> <td><TotalFinanceLeaseLiabilitiesJune302022>14,902 </TotalFinanceLeaseLiabilitiesJune302022></td></tr> <tr><td><OperatingLeasesWeightedAverageRemainingLeaseTerm>Weighted Average Remaining Lease Term </OperatingLeasesWeightedAverageRemainingLeaseTerm></td><td/><td><WeightedAverageRemainingLeaseTermJune302022/></td></tr> <tr><td><OperatingLeasesOperatingLeases>Operating leases </OperatingLeasesOperatingLeases></td> <td><OperatingLeasesDecember312022>8 years </OperatingLeasesDecember312022></td> <td><OperatingLeasesJune302022>8 years </OperatingLeasesJune302022></td></tr> <tr><td><OperatingLeasesFinanceLeases>Finance leases </OperatingLeasesFinanceLeases></td> <td><FinanceLeasesDecember312022>11 years </FinanceLeasesDecember312022></td> <td><FinanceLeasesJune302022>12 years </FinanceLeasesJune302022></td></tr> <tr><td><OperatingLeasesWeightedAverageDiscountRate>Weighted Average Discount Rate </OperatingLeasesWeightedAverageDiscountRate></td><td/><td><WeightedAverageDiscountRateJune302022/></td></tr> <tr><td><OperatingLeasesOperatingLeases>Operating leases </OperatingLeasesOperatingLeases></td> <td><OperatingLeasesDecember312022>2.\n",
      "2023 Q2 NVDA.pdf\n",
      "</OperatingLeaseCommitments>\n",
      " <OperatingLeaseExpenses> Operating lease expenses were <OperatingLeaseExpenses>$67 million </OperatingLeaseExpenses>and <OperatingLeaseExpenses>$47 million </OperatingLeaseExpenses>for the second quarter of fiscal years <OperatingLeaseExpenses>2024 </OperatingLeaseExpenses>and 2023, respectively, and <OperatingLeaseExpenses>$126 million </OperatingLeaseExpenses>and <OperatingLeaseExpenses>$90 million </OperatingLeaseExpenses>for the first half of fiscal years 2024 and 2023, respectively. Short-term and variable lease expenses for the second quarter and first half of fiscal years 2024 and <OperatingLeaseExpenses>2023 </OperatingLeaseExpenses>were not significant. </OperatingLeaseExpenses>\n",
      " <Number>10 </Number>\n",
      " Other information related to leases was as follows:\n",
      " <table><tbody><tr><td><SixMonthsEnded-cell><SixMonthsEnded>Six Months </SixMonthsEnded>Ended <July302023-section> <Deadline>July 30, 2023 </Deadline><July302023> <Deadline>July 31, 2022 </Deadline> (In millions) </July302023></July302023-section></SixMonthsEnded-cell></td></tr> <tr><td><SixMonthsEndedJuly302023July312022InMillionsSupplementalCashFlowsI>Supplemental cash flows information </SixMonthsEndedJuly302023July312022InMillionsSupplementalCashFlowsI></td></tr> <tr><td><SixMonthsEndedJuly302023July312022InMillionsOperatingCashFlowsUsed>Operating cash flows used for operating leases </SixMonthsEndedJuly302023July312022InMillionsOperatingCashFlowsUsed></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 135 </OperatingCashFlowsUsedforOperatingLeases></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 91 </OperatingCashFlowsUsedforOperatingLeases></td></tr> <tr><td><SixMonthsEndedJuly302023July312022InMillionsOperatingLeaseAssetsOb>Operating lease assets obtained in exchange for lease obligations </SixMonthsEndedJuly302023July312022InMillionsOperatingLeaseAssetsOb></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 299 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 98 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td></tr></tbody></table>\n",
      " <OperatingLeaseDetails> As of <LeaseTerm>July 30, 2023</LeaseTerm>, our operating leases had a weighted average remaining lease term of <RemainingLeaseTerm>6.5 years </RemainingLeaseTerm>and a weighted average discount rate of <WeightedAverageDiscountRate>3.47%</WeightedAverageDiscountRate>.\n",
      "2023 Q1 NVDA.pdf\n",
      "</OperatingLeaseCommitments>\n",
      " <OperatingLeaseExpenses> Operating lease expenses were <OperatingLeaseExpenses>$59 million </OperatingLeaseExpenses>and <OperatingLeaseExpenses>$44 million </OperatingLeaseExpenses>for the first quarter of fiscal years <OperatingLeaseExpenses>2024 </OperatingLeaseExpenses>and 2023, respectively. Short-term and variable lease expenses for the first quarter of fiscal years 2024 and <OperatingLeaseExpenses>2023 </OperatingLeaseExpenses>were not significant. </OperatingLeaseExpenses>\n",
      " Other information related to leases was as follows:\n",
      " <table><tbody><tr><td><ThreeMonthsEnded-section><ThreeMonthsEnded>Three Months </ThreeMonthsEnded>Ended <ThreeMonthsEnded> <Deadline>April 30, 2023 </Deadline><Deadline>May 1, 2022 </Deadline> (In millions) </ThreeMonthsEnded></ThreeMonthsEnded-section></td></tr> <tr><td><ThreeMonthsEndedApril302023May12022InMillionsSupplementalCashFlows>Supplemental cash flows information </ThreeMonthsEndedApril302023May12022InMillionsSupplementalCashFlows></td></tr> <tr><td><ThreeMonthsEndedApril302023May12022InMillionsOperatingCashFlowsUse>Operating cash flows used for operating leases </ThreeMonthsEndedApril302023May12022InMillionsOperatingCashFlowsUse></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 61 </OperatingCashFlowsUsedforOperatingLeases></td> <td><OperatingCashFlowsUsedforOperatingLeases>$ 45 </OperatingCashFlowsUsedforOperatingLeases></td></tr> <tr><td><ThreeMonthsEndedApril302023May12022InMillionsOperatingLeaseAssetsO>Operating lease assets obtained in exchange for lease obligations </ThreeMonthsEndedApril302023May12022InMillionsOperatingLeaseAssetsO></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 106 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td> <td><OperatingLeaseAssetsObtainedinExchangeforLeaseObligations>$ 62 </OperatingLeaseAssetsObtainedinExchangeforLeaseObligations></td></tr></tbody></table>\n",
      " <Number>9 </Number>\n",
      " <OperatingLeaseDetails> As of <LeaseTerm>April 30, 2023</LeaseTerm>, our operating leases had a weighted average remaining lease term of <RemainingLeaseTerm>6.6 years </RemainingLeaseTerm>and a weighted average discount rate of <WeightedAverageDiscountRate>3.33%</WeightedAverageDiscountRate>. As of <LeaseTermandDiscountRate>January 29, 2023</LeaseTermandDiscountRate>, our operating leases had a weighted average remaining lease term of <WeightedAverageLeaseTerm>6.8 years </WeightedAverageLeaseTerm>and a weighted average discount rate of <WeightedAverageDiscountRate>3.21%</WeightedAverageDiscountRate>.\n",
      "2023 Q2 MSFT.pdf\n",
      "We expect to recognize approximately <RecognitionRate>45% </RecognitionRate>of this revenue over the next <RecognitionPeriod>12 months </RecognitionPeriod>and the remainder thereafter. </_39601>\n",
      " NOTE 13 — LEASES\n",
      " <LeaseTerms> We have operating and finance leases for datacenters, corporate offices, research and development facilities, <LeaseType>Microsoft Experience Centers</LeaseType>, and certain equipment. Our leases have remaining lease terms of less than <TimeDuration>1 year </TimeDuration>to <LeaseTerm>18 years</LeaseTerm>, some of which include options to extend the leases for up to <LeaseTerm>5 years</LeaseTerm>, and some of which include options to terminate the leases within <LeaseTerm>1 year</LeaseTerm>. </LeaseTerms>\n",
      " The components of lease expense were as follows:\n",
      " <table><tbody><tr><td>(In millions) </td> <td><ThreeMonthsEndedMarch31-cell><ThreeMonthsEnded>Three Months </ThreeMonthsEnded>Ended <March31-section> <Deadline>March 31</Deadline>, <March31> <Number>2023 </Number> <Number>2022 </Number></March31></March31-section></ThreeMonthsEndedMarch31-cell></td> <td><NineMonthsEnded-section><NineMonthsEnded>Nine Months </NineMonthsEnded>Ended <NineMonthsEnded> <Deadline>March 31</Deadline>, <March31> <Number>2023 </Number> <Number>2022 </Number></March31></NineMonthsEnded></NineMonthsEnded-section></td></tr> <tr><td><InMillions>Operating lease cost </InMillions></td> <td><OperatingLeaseCostThreeMonthsEndedMarch3120232022><Money>$ 766 </Money> <Money>$ </Money></OperatingLeaseCostThreeMonthsEndedMarch3120232022></td> <td><OperatingLeaseCostNineMonthsEndedMarch3120232022><Number>744 </Number> <Money>$ </Money></OperatingLeaseCostNineMonthsEndedMarch3120232022></td> <td><OperatingLeaseCost><Number>2,112 </Number> <Money>$ </Money></OperatingLeaseCost></td> <td><OperatingLeaseCost>1,\n",
      "2023 Q3 MSFT.pdf\n",
      "The estimated fair values are based on Level <EstimatedFairValueofLong-TermDebt>2 </EstimatedFairValueofLong-TermDebt>inputs. </Long-TermDebtFairValue>\n",
      " <DebtStructure> Debt in the table above is comprised of senior unsecured obligations and ranks equally with our other outstanding obligations. Interest is paid semi-<Frequency>annually</Frequency>, except for the Euro-denominated debt, which is paid <Frequency>annually</Frequency>. </DebtStructure>\n",
      " <Long-TermDebtMaturitySchedule> The following table outlines maturities of our long-term debt, including the current portion, as of <MaturityDate>September 30, 2023</MaturityDate>: </Long-TermDebtMaturitySchedule>\n",
      " (In millions)\n",
      " <table><tbody><tr><td>Year Ending <YearEndDate>June 30</YearEndDate>, </td><td><YearEndingJune30/></td></tr> <tr><td><YearEndingJune302024ExcludingtheThreeMonthsEndedSeptember302023><EndDate>2024 </EndDate>(excluding the <EndDate>three months </EndDate>ended <EndDate>September 30, 2023</EndDate>) </YearEndingJune302024ExcludingtheThreeMonthsEndedSeptember302023></td> <td><_2024ExcludingtheThreeMonthsEndedSeptember302023>$ 3,750 </_2024ExcludingtheThreeMonthsEndedSeptember302023></td></tr> <tr><td><YearEndingJune302025>2025 </YearEndingJune302025></td> <td><_2025>2,250 </_2025></td></tr> <tr><td><YearEndingJune302026>2026 </YearEndingJune302026></td> <td><_2026>3,000 </_2026></td></tr> <tr><td><YearEndingJune302027>2027 </YearEndingJune302027></td> <td><_2027>8,000 </_2027></td></tr> <tr><td><YearEndingJune302028>2028 </YearEndingJune302028></td> <td><_2028>0 </_2028></td></tr> <tr><td><YearEndingJune30Thereafter>Thereafter </YearEndingJune30Thereafter></td> <td><Thereafter>34,292 </Thereafter></td></tr><tr><td><YearEndingJune30/></td></tr> <tr><td><YearEndingJune30Total>Total </YearEndingJune30Total></td> <td><Total>$ 51,292 </Total></td></tr></tbody></table>\n",
      " <Number>19 </Number>\n",
      " NOTE 10 — INCOME TAXES\n",
      " Effective Tax Rate\n",
      " <TaxRate> Our effective tax rate was <EffectiveTaxRate>18% </EffectiveTaxRate>and <EffectiveTaxRate>19% </EffectiveTaxRate>for the <EffectiveTaxRate>three months </EffectiveTaxRate>ended <EffectiveTaxRate>September 30, 2023 </EffectiveTaxRate>and <EffectiveTaxRate>2022</EffectiveTaxRate>, respectively. The decrease in our effective tax rate for the current quarter compared to the prior year was primarily due to tax benefits from tax law changes in the <EffectiveTaxRate>first quarter of fiscal year 2024</EffectiveTaxRate>, including the impact from the issuance of Notice <EffectiveTaxRate>2023</EffectiveTaxRate>-<EffectiveTaxRate>55 </EffectiveTaxRate>by the <TaxLawChanges>Internal Revenue Service </TaxLawChanges>(“<IRSNotice>IRS”</IRSNotice>) and <USTreasuryDepartment>U.S. Treasury Department</USTreasuryDepartment>, which delayed the effective date of final foreign tax credit regulations to fiscal <EffectiveTaxRate>year </EffectiveTaxRate><EffectiveTaxRate>2024 </EffectiveTaxRate>for <EffectiveTaxRate>Microsoft</EffectiveTaxRate>. </TaxRate>\n"
     ]
    }
   ],
   "source": [
    "for node in response.source_nodes:\n",
    "    print(node.node.extra_info[\"name\"])\n",
    "    print(node.node.text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
